// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// o [author] rhyme_lph
// S [email]  rhymelph@gmail.com
// q [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/entities/income_type_entity.dart';
import 'package:flutter/material.dart';

class SelectWorkingDayProvider extends BaseProvider<List<IncomeTypeEntity>> {
  num workingDaysCount;
  List<WorkingDayData> workingDays = [];

  @override
  Future get dataFuture => null;

  void onSave() {
    num appliedDays = 0;
    workingDays.forEach((workingDay) {
      if (workingDay.isWorking == 1 && workingDay.isHalfDay == 0)
        appliedDays += 1;
      if (workingDay.isWorking == 1 && workingDay.isHalfDay == 1)
        appliedDays += 0.5;
    });
    if (appliedDays != workingDaysCount) {
      showToast(S.current.please_select_the_working_days);
    } else {
      obtainContext?.call((context) {
        Navigator.of(context).pop(
            {"workingDaysCount": workingDaysCount, "workingDays": workingDays});
      });
    }
  }

  void onChangeNumberOfWorkingDays(num days) {
    workingDaysCount = days;
    workingDays = getDefaultWorkingDays(days);
    update();
  }

  void onChangeIsWorkingDay(WorkingDayData data) {
    if (data.isWorking == 1 && data.isHalfDay == 1) {
      data.isHalfDay = 0;
    } else if (data.isWorking == 1 && data.isHalfDay == 0) {
      data.isWorking = 0;
    } else {
      data.isWorking = 1;
      data.isHalfDay = 0;
    }
    update();
  }

  void onChangeIsHalfDay(WorkingDayData data) {
    if (data.isWorking == 1 && data.isHalfDay == 1) {
      data.isHalfDay = 0;
      data.isWorking = 0;
    } else {
      data.isWorking = 1;
      data.isHalfDay = 1;
    }
    update();
  }

  List<WorkingDayData> getDefaultWorkingDays(num days) {
    return [
      WorkingDayData()
        ..isWorking = 1
        ..isHalfDay = 0,
      WorkingDayData()
        ..isWorking = 1
        ..isHalfDay = 0,
      WorkingDayData()
        ..isWorking = 1
        ..isHalfDay = 0,
      WorkingDayData()
        ..isWorking = 1
        ..isHalfDay = 0,
      WorkingDayData()
        ..isWorking = 1
        ..isHalfDay = 0,
      WorkingDayData()
        ..isWorking = days == 6 || days == 5.5 ? 1 : 0
        ..isHalfDay = days == 5.5 ? 1 : 0,
      WorkingDayData()
        ..isWorking = 0
        ..isHalfDay = 0,
    ];
  }
}
